package com.xinchao.controller;

import com.xinchao.core.BaseController;
import com.xinchao.core.constant.ResponseConstant;
import com.xinchao.model.dto.AreaCityDTO;
import com.xinchao.model.dto.AreaTreeDTO;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.model.vo.AreaVO;
import com.xinchao.service.AreaService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author dxy
 * @date 2018/8/9 10:36
 */
@Controller
@RequestMapping(value = "/area")
public class AreaController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(AreaController.class);
	@Autowired
	private AreaService areaService;

	/**
	 * 获取所有省
	 */
	@ResponseBody
	@RequestMapping(value = "/listAllProvince")
	public Object listAllProvince() {
		return areaService.listAllProvince();
	}

	/**
	 * 获取省的城市列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listCity")
	public Object listCity(String provinceCode) {
		if (StringUtils.isBlank(provinceCode)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "省编码不能为空");
		}
		return areaService.listCityByProvince(provinceCode);
	}

	/**
	 * 获取部门的城市列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listDepartmentAndSelectedCity")
	public Object listDepartmentAndSelectedCity(Long departmentId) {
		Map<String, Object> cityMap = new HashMap<>();
		//部门关联的城市
		List<AreaCityDTO> areaCityList = null;
		if (departmentId == null) {
			cityMap.put("city", new ArrayList<>());
		} else {
			areaCityList = areaService.listCityByDeparmentId(departmentId);
			cityMap.put("city", areaCityList);
		}
		//所有已经被关联的城市
		List<AreaCityDTO> selectedCityList = areaService.listAllSelectedCity();
		//本部门管理的城市，不放入禁用列表
		Set<AreaCityDTO> tempSet = new HashSet<>();
		//标识要放入临时set的标识
		boolean flag = true;
		if (CollectionUtils.isNotEmpty(areaCityList) && CollectionUtils.isNotEmpty(selectedCityList)) {
			for (int j = 0; j < selectedCityList.size(); j++) {
				AreaCityDTO selectedCity = selectedCityList.get(j);
				String selectedCode = selectedCity.getCode();
				if (StringUtils.isBlank(selectedCode)) {
					continue;
				}
				for (int i = 0; i < areaCityList.size(); i++) {
					AreaCityDTO areaCity = areaCityList.get(i);
					String areaCode = areaCity.getCode();
					if (StringUtils.isBlank(areaCode)) {
						continue;
					}
					if (areaCode.equals(selectedCode)) {
						flag = false;
						break;
					}
				}
				if (flag) {
					tempSet.add(selectedCity);
				}
			}
			cityMap.put("disabled", tempSet);
		} else {
			cityMap.put("disabled", selectedCityList);
		}
		return cityMap;
	}

	/**
	 * 获取部门的城市列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listCityByDeparmentId")
	public Object listCityByDeparmentId(Long departmentId) {
		if (departmentId == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "部门ID不能为空");
		}
		return areaService.listCityByDeparmentId(departmentId);
	}


	/**
	 * 省市区下拉联动查询
	 * 参数都为空时，查询所有省份，通过省份查市，通过市查县/区
	 *
	 * @param province 省
	 * @param city     市
	 * @return Object
	 * @author linqiang
	 * @date 2018-8-16 09:54
	 */
	@ResponseBody
	@RequestMapping(value = "/list")
	public Object list(String province, String city){
		try {
			List<AreaVO> areaList = areaService.list(province, city);
			return new JsonResponse(ResponseConstant.CODE_SUCCESS, areaList,"success");
		} catch (Exception e) {
			logger.error("AreaController list 获取省市区失败：" + e.getMessage());
		}
		return new JsonResponse(ResponseConstant.CODE_FAIL, "fail");
	}

	@ResponseBody
	@RequestMapping(value = "/listTree")
	public Object listTree(){
		return areaService.listAllTreeData();
	}

	@ResponseBody
	@RequestMapping(value = "/listTreeByDepartmentId")
	public Object listTreeByDepartmentId(Long departmentId, HttpServletRequest request){
        Map<String, Object> result = new HashMap<>(2);
		List<AreaTreeDTO> data = new ArrayList<>();
        if(null == departmentId){
			data = areaService.listAllTreeData();
		} else {
			data = areaService.listAllTreeDataByDepartmentId(departmentId);
		}
		result.put("data", data);
		String cityName = getCurrentCityName(request);
		result.put("defaultCityName", StringUtils.isBlank(cityName) ? "" : cityName);
		return new JsonResponse(ResponseConstant.CODE_SUCCESS, result,"success");
	}


}
